package com.example.demo.controller;


import cn.hutool.core.lang.UUID;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.entity.Body;
import com.example.demo.entity.Header;
import com.example.demo.entity.JsonRootBean;
import com.example.demo.utils.RSAUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 小坏
 */
@RestController
public class ＷebController {


    @Value("${hxx.appId}")
    private String hxxAppId;

    @Value("${hxx.appKey}")
    private String hxxAppKey;

    @Value("${hxx.version}")
    private String hxxVersion;

    @Value("${hxx.publicKey}")
    private String hxxPublicKey;

    @Value("${hxx.privateKey}")
    private String hxxPrivateKey;


    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/getName")
    public JSONObject getName(@RequestParam("phone") String phone,
                              @RequestParam("token") String token,
                              @RequestParam("userInformation") String userInformation) throws Exception {

        JsonRootBean jsonRootBean = new JsonRootBean();
        Header header = new Header();
        header.setVersion(hxxVersion);
        header.setAppId(hxxAppId);

        Body body = new Body();

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);

        //使用 traceId 标识请求的唯一性（32 位） 生成 traceId
        String traceId = UUID.randomUUID().toString().replaceAll("-", "");
        header.setMsgId(traceId);

        //请求消息发送的系统时间，精确到毫秒，共17位，格式：20121227180001165
        String timestamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        header.setTimestamp(timestamp);

        //封装三个参数进行加密
        String concat = phone + hxxAppKey + timestamp;
        // 公钥加密密钥A，此处为55555
        String phoneNum = RSAUtils.encryptedDataOnJava(concat, hxxPublicKey);

        body.setPhoneNum(phoneNum);
        body.setToken(token);
        //签名，业务方RSA客户私钥生成签名(appId+msgId+phoneNum+timestamp+token+version)

        String sign = hxxAppId + traceId + phoneNum + timestamp + token + hxxVersion;


        //待验签
        String sign2 = RSAUtils.sign(sign.getBytes(), hxxPrivateKey);
        body.setSign(sign2);
        body.setUserInformation(userInformation);

//        System.out.println("version: " + hxxVersion);
//        System.out.println("traceId: " + traceId);
//        System.out.println("timestamp: " + timestamp);
//        System.out.println("appId: " + hxxAppId);
//        System.out.println("phoneNum: " + phoneNum);
//        System.out.println("token: " + token);
//        System.out.println("sign: " + sign2);
//        System.out.println("userInformation: " + userInformation);

        jsonRootBean.setHeader(header);
        jsonRootBean.setBody(body);
        String data = JSONObject.toJSONString(jsonRootBean);
        System.out.println("json: " + data);

        HttpEntity<String> request = new HttpEntity<String>(data, httpHeaders);

        ResponseEntity<JSONObject> stringResponseEntity = restTemplate.postForEntity("https://www.cmpassport.com/NumberAbility/h5/verifyVmMobileApi.htm", request, JSONObject.class);
        return stringResponseEntity.getBody();
    }

}
